package cn.edu.scau.cmi.liangzijian.composite.consistent;

import java.net.URL;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.liangzijian.composite.consistent.ConsistentOrganization;
import cn.edu.scau.cmi.liangzijian.composite.consistent.ConsistentPerson;
import cn.edu.scau.cmi.liangzijian.composite.consistent.ConsistentTeam;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;


public class UIController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Label addTip;

    @FXML
    private TextArea board;

    @FXML
    private Label deleteTip;

    @FXML
    private Button returnLast;

    @FXML
    private TextField toAddName;

    @FXML
    private TextField toDeleteName;

    @FXML
    private TextField toVisitName;

    @FXML
    private ChoiceBox<String> type;

    private String typeString = null;
    
    @FXML
    private Label visitTip;

    private ConsistentOrganization root = new ConsistentTeam("root");
    
    private ConsistentOrganization current = root;
    
    @FXML
    void add(MouseEvent event) {
    	addTip.setText("");
    	String toAddNameString = toAddName.getText().trim();
    	
    	if(toAddNameString.equals("")) {
    		addTip.setText("名称不能为空");
    	} else {
    		boolean flag = false;
    		
    		if(typeString.equals("个人")) {
    			flag = current.add(new ConsistentPerson(toAddNameString));
    			System.out.println(flag);
    		} else {
    			flag = current.add(new ConsistentTeam(toAddNameString));
    		}
    		
    		if(!flag) {
        		addTip.setText("无法添加");
    		} else {
        		display(root, current);
    		}
    		toAddName.setText("");
    	}
    }

    @FXML
    void delete(MouseEvent event) {
    	deleteTip.setText("");
    	String toDeleteNameString = toDeleteName.getText().trim();
    	
    	if(toDeleteNameString.equals("")) {
    		deleteTip.setText("名称不能为空");
    	} else {
    		if(current.remove(toDeleteNameString)) {
    			display(root, current);
    		} else {
    			deleteTip.setText("无法删除");
    		}
    		toDeleteName.setText("");
    	}
    }

    @FXML
    void visit(MouseEvent event) {
    	visitTip.setText("");
    	String toVisitNameString = toVisitName.getText().trim();
    	
    	if(toVisitNameString.equals("")) {
    		visitTip.setText("名称不能为空");
    	} else {
    		ConsistentOrganization so = current.getOrganization(toVisitNameString);
    		
    		if(so != null) {
	    		current = so;
	    		display(root, current);
    		} else {
    			visitTip.setText("无法访问");
    		}
    		toVisitName.setText("");
    	}
    }
    
    @FXML
    void returnLast() {
    	current = current.getParent();
    	display(root, current);
    }

    @FXML
    void initialize() {
        assert addTip != null : "fx:id=\"addTip\" was not injected: check your FXML file 'UI.fxml'.";
        assert board != null : "fx:id=\"board\" was not injected: check your FXML file 'UI.fxml'.";
        assert deleteTip != null : "fx:id=\"deleteTip\" was not injected: check your FXML file 'UI.fxml'.";
        assert returnLast != null : "fx:id=\"returnLast\" was not injected: check your FXML file 'UI.fxml'.";
        assert toAddName != null : "fx:id=\"toAddName\" was not injected: check your FXML file 'UI.fxml'.";
        assert toDeleteName != null : "fx:id=\"toDeleteName\" was not injected: check your FXML file 'UI.fxml'.";
        assert toVisitName != null : "fx:id=\"toVisitName\" was not injected: check your FXML file 'UI.fxml'.";
        assert type != null : "fx:id=\"type\" was not injected: check your FXML file 'UI.fxml'.";
        assert visitTip != null : "fx:id=\"visitTip\" was not injected: check your FXML file 'UI.fxml'.";

        type.setItems(FXCollections.observableArrayList("个人", "团队"));
        type.valueProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue observable, Object oldValue, Object newValue) {
				typeString = (String)newValue;
			}    	
    	});
        
        
        root.setParent(root);
        display(root, root);
    }
    
    public void display(ConsistentOrganization sm, ConsistentOrganization target) {
    	board.clear();
    	dfs(sm, target, 1);
    }
    
    public void dfs(ConsistentOrganization sm, ConsistentOrganization target, int level) {
    	for(int i = 0; i < level; i++) 
    		board.appendText((i == 0 ? "|" : "  |"));
		
    	board.appendText("—— ");
		if(!sm.getName().equals("root")) {
			if(sm instanceof ConsistentTeam)
				board.appendText("（团队）");
			else
				board.appendText("（个人）");
			board.appendText(sm.getName());
		}
		if(sm == target)
			board.appendText("←");
		board.appendText("\n");
		
		if(sm instanceof ConsistentTeam) {	
			for(ConsistentOrganization m : ((ConsistentTeam)sm).getMemberList()) {
				dfs(m, target, level+1);
			} 
		}
    }

}
